package com.rmy.queue.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rmy.common.core.lang.IdGenerator;
import com.rmy.common.core.text.Convert;
import com.rmy.common.utils.StringUtils;
import com.rmy.common.utils.ip.IpUtils;
import com.rmy.common.utils.uuid.IdUtils;
import com.rmy.queue.domain.QmsBerrConfig;
import com.rmy.queue.domain.QmsBusinessConfig;
import com.rmy.queue.domain.QmsWindowBusiness;
import com.rmy.queue.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rmy.common.annotation.Log;
import com.rmy.common.core.controller.BaseController;
import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.enums.BusinessType;
import com.rmy.queue.domain.QmsWindowInfo;
import com.rmy.common.utils.poi.ExcelUtil;
import com.rmy.common.core.page.TableDataInfo;

import static com.rmy.common.utils.ip.Ip.getExactlocalAddress;

/**
 * 鹤位信息Controller
 *
 * @author rmy
 * @date 2022-09-29
 */
@RestController
@RequestMapping("/queue/windowinfo")
public class QmsWindowInfoController extends BaseController
{
    @Autowired
    private IQmsWindowInfoService qmsWindowInfoService;

    @Autowired
    IQmsWindowBusinessService qmsWindowBusinessService;

    @Autowired
    private IQmsBerrConfigService qmsBerrConfigService;

    @Autowired
    private IQmsWindowBerrService qmsWindowBerrService;


    /**
     * 查询鹤位信息列表
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(QmsWindowInfo qmsWindowInfo)
    {
        startPage();
        List<QmsWindowInfo> list = qmsWindowInfoService.selectQmsWindowInfoList(qmsWindowInfo);
        return getDataTable(list);
    }

    /**
     * 导出鹤位信息列表
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:export')")
    @Log(title = "鹤位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsWindowInfo qmsWindowInfo)
    {
        List<QmsWindowInfo> list = qmsWindowInfoService.selectQmsWindowInfoList(qmsWindowInfo);
        ExcelUtil<QmsWindowInfo> util = new ExcelUtil<QmsWindowInfo>(QmsWindowInfo.class);
        util.exportExcel(response, list, "鹤位信息数据");
    }

    /**
     * 获取鹤位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        QmsWindowInfo qmsWindowInfo = qmsWindowInfoService.getDetailInfo(id);
        return AjaxResult.success(qmsWindowInfo);
    }

    /**
     * 新增鹤位信息
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:add')")
    @Log(title = "鹤位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsWindowInfo qmsWindowInfo) throws Exception {
        return toAjax(qmsWindowInfoService.addQmsWindowInfo(qmsWindowInfo));
    }

    /**
     * 修改鹤位信息
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:edit')")
    @Log(title = "鹤位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsWindowInfo qmsWindowInfo)
    {
        return toAjax(qmsWindowInfoService.editQmsWindowInfo(qmsWindowInfo));
    }

    /**
     * 删除鹤位信息
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:remove')")
    @Log(title = "鹤位信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        qmsWindowBerrService.deleteQmsWindowBerrByWindowIds(ids);
        qmsWindowBusinessService.deleteQmsWindowBusinessByWindowIds(ids);
        return toAjax(qmsWindowInfoService.deleteQmsWindowInfoByIds(ids));
    }


    /**
     * 根据ID获取登录地址
     */
    @GetMapping(value = "/getWindowLoginUrl/{id}")
    public AjaxResult getWindowLoginUrl(@PathVariable("id") Long id, @RequestParam(required = false) String ipPort)
    {
        QmsWindowInfo  qmsWindowInfo = qmsWindowInfoService.selectQmsWindowInfoById(id);
        if (null == qmsWindowInfo) {
            return AjaxResult.error("未查询到鹤位信息，请刷新后重试！！");
        }
        String strServerIp = ipPort;
        if (ipPort == null || "".equals(ipPort)) {
            strServerIp = getExactlocalAddress().getHostAddress();
        }
        String strProctol = "http://";
        String strPathUrl = "/callmanage?uuid="+qmsWindowInfo.getUuid();
        String strAllPath = strProctol + strServerIp + strPathUrl;
        return AjaxResult.success((Object)strAllPath);
    }

    /**
     * 根据UUID获取鹤位信息
     */
    @GetMapping(value = "/getWindowInfoByUUID")
    public AjaxResult getWindowInfoByUUID(@RequestParam String uuid) {
        QmsWindowInfo qmsWindowInfo = qmsWindowInfoService.selectQmsWindowInfoByUUID(uuid);
        if (null == qmsWindowInfo) {
            return AjaxResult.error("请查看后台配置，重新获取鹤位登录地址！");
        }
        // 不等于暂停服务
        if (!"3".equals(qmsWindowInfo.getRunningStatus())) {
            qmsWindowInfo.setRunningStatus("2");
            qmsWindowInfoService.updateQmsWindowInfo(qmsWindowInfo);
        }

        return AjaxResult.success(qmsWindowInfo);
    }


    /**
     * 改变窗口状态
     */
    @PutMapping(value = "/changeRunningStatus")
    public AjaxResult changeRunningStatus(@RequestBody QmsWindowInfo qmsWindowInfo)
    {
        return toAjax(qmsWindowInfoService.updateQmsWindowInfo(qmsWindowInfo));
    }

    /**
     * 校验机构
     * data为1 代表校验成功 0校验失败
     */
    @PostMapping(value = "/checkDept")
    public AjaxResult checkDept(@RequestParam String uuid)
    {
        Long lDeptId = getDeptId();
        QmsWindowInfo qmsWindowInfo = new QmsWindowInfo();
        qmsWindowInfo.setOrgNo(Convert.toStr(lDeptId));
        qmsWindowInfo.setUuid(uuid);
        List<QmsWindowInfo> qmsWindowInfoList = qmsWindowInfoService.selectQmsWindowInfoList(qmsWindowInfo);
        if (qmsWindowInfoList == null || qmsWindowInfoList.size() == 0){
            return AjaxResult.success((Object)"0");
        }
        return AjaxResult.success((Object)"1");
    }


    /**
     * 查询绑定的鹤位屏信息
     */
    @GetMapping("/bindedBerrList")
    public TableDataInfo bindedBerrList(QmsBerrConfig qmsBerrConfig)
    {
        startPage();
        List<QmsBerrConfig> list = qmsBerrConfigService.selectBindedBerrList(qmsBerrConfig);
        return getDataTable(list);
    }

    /**
     * 查询未绑定的鹤位屏信息
     */
    @GetMapping("/unbindedBerrList")
    public TableDataInfo unbindedBerrList(QmsBerrConfig qmsBerrConfig)
    {
        startPage();
        List<QmsBerrConfig> list = qmsBerrConfigService.selectUnbindedBerrList(qmsBerrConfig);
        return getDataTable(list);
    }

    /**
     * 批量选择鹤位屏绑定
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:edit')")
    @PutMapping("/bindSelectBerr")
    public AjaxResult bindSelectBerr(Long windowId, Long[] berrIds)
    {
        return AjaxResult.success(qmsWindowInfoService.insertBindBerrs(windowId, berrIds));
    }

    /**
     * 批量取消鹤位屏绑定
     */
    @PreAuthorize("@ss.hasPermi('queue:windowinfo:edit')")
//    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/unbindSelectBerr")
    public AjaxResult unbindSelectBerr(Long windowId, Long[] berrIds)
    {
        return toAjax(qmsWindowInfoService.deleteWindowBerrs(windowId, berrIds));
    }


}
